#include <stdio.h>
#include <acldev.h>
#include <aclutil.h>
#include <xmodem.h>
#include <string.h>
#include <aclconio.h>
#include <Prot8583.h>
#include <stdlib.h>

#include <ascii.h>
#include <applidl.h>
#include <svc.h>
#include <device.h>

//#include "C:\Verixaps\VMAC\include\logsys.h"
//#include "C:\VerixVaps\VMAC\ver150\include\logsys.h"
#include <logsys.h>

#include "UserIf.h"
#include "Comm.h"
#include "uclcode.h"
#include "Display.txo"

COMM_OBJECT obCommunication;
static int siMemUsage=0;
extern int hLAN;	//Handle for TCPIP modem
extern int spipe;
extern int remoteTaskID;
extern int remotePipe;

int inSmartDelay ( unsigned int Delay );

char *pchAlloc (unsigned unSize)
{
	char *ptr;	
	ptr = calloc(1, unSize);
	++siMemUsage;
	LOG_PRINTFF((0x08L, "pchAlloc...Memory Usage = %d", siMemUsage));
	return(ptr);
}

void vdFree (char *ptr)
{
	if (ptr) {
		--siMemUsage;
		free(ptr);
	}
	LOG_PRINTFF((0x08L, "vdFree...Memory Usage = %d", siMemUsage));
}

int inBeginComm(void)
{
	if (obCommunication.inBegin) {
        if (obCommunication.inBegin(&obCommunication, 1 , VS_TRUE) < VS_SUCCESS) {
			DisplayStatus("Connection Failed!");
            return 0;
        }
    }
	return 1;
}

int inEndComm(void)
{
	if (obCommunication.inEnd){
		if (obCommunication.inEnd(&obCommunication) != VS_SUCCESS) {
			DisplayStatus("Error in disconnect!");
			SVC_WAIT(1000);
		}
	}
	return 1;
}

int inReleaseComm(void)
{

	if (obCommunication.inEnd){
		obCommunication.inEnd(&obCommunication);
	}
	if ( obCommunication.inDeinitialize ){
		obCommunication.inDeinitialize(&obCommunication);   
    }

	return 1;
}

int inDisplaySignalStrengthInterface(void)
{
	return inUpdateWirelessStatus();
}

int inDisplaySignalStrengthInternalInterface(void)
{
	return inHostConnectionTest();
}

int inInitializeComm (int inCommLinkType)
{
	int     inRetVal = VS_SUCCESS;

    if ( obCommunication.inLinkType != inCommLinkType ) {
		if(obCommunication.inLinkType != NO_COMM) {
	        if ( obCommunication.inDeinitialize ){
				obCommunication.inDeinitialize(&obCommunication);   
        	}
        }    
        switch ( inCommLinkType ) {
            case LANDLINE_COMM:
			case RS232_COMM:
			case GSM_COMM:
			case IP_COMM:
			case WIFI_COMM:
			case GPRS_COMM:
            case CDMA_COMM:
                vdInitializeUclComm (&obCommunication);
                break;
            case MODEM_COMM:
                vdInitializeModemComm (&obCommunication);
                break;                
            case TCP_IP_COMM:
                vdInitializeTcpIpComm (&obCommunication);
                break;
            default:
				return(VS_ERR);
                break;
        }        
        //inRetVal = obCommunication.inInitialize (&obCommunication);
    }
	inRetVal = obCommunication.inInitialize (&obCommunication);  //for MultiApp
    return ( inRetVal );
}


void vdInitializeModemComm (COMM_OBJECT *obCommunication)
{
    obCommunication->inLinkType	= MODEM_COMM;
    obCommunication->fLinkUp = VS_FALSE;

#ifdef __arm    
    obCommunication->inInitialize	= (PFI_VD) inInitModemCommunication;
    obCommunication->inBegin		= (PFI_PFOR_BEGIN) inBeginModemCommunication;
    obCommunication->inCheck		= (PFI_VD) inCheckModemCommunication;
    obCommunication->inSend			= (PFI_PFOR_SEND) inSendModemCommunication;
    obCommunication->inReceive		= (PFI_PFOR_RES) inReceiveModemCommunication;
    obCommunication->inEnd			= (PFI_VD) inEndModemCommunication;
    obCommunication->inFlush		= (PFI_VOID) inFlushModemCommunication;
    obCommunication->inDeinitialize = (PFI_VD) inDeInitModemCommunication;
#elif _TARG_68000
    obCommunication->inInitialize	= inInitModemCommunication;
    obCommunication->inBegin		= inBeginModemCommunication;
    obCommunication->inCheck		= inCheckModemCommunication;
    obCommunication->inSend			= inSendModemCommunication;
    obCommunication->inReceive		= inReceiveModemCommunication;
    obCommunication->inEnd			= inEndModemCommunication;
    obCommunication->inFlush		= inFlushModemCommunication;
    obCommunication->inDeinitialize = inDeInitModemCommunication;
#endif
	obCommunication->inSendReceive  = NULL;
}

void vdInitializeTcpIpComm (COMM_OBJECT *obCommunication)
{
    obCommunication->inLinkType	= TCP_IP_COMM;
    obCommunication->fLinkUp = VS_FALSE;
	obCommunication->inInitialize   = inInitTcpIpCommunication;
	obCommunication->inBegin        = inBeginTcpIpCommunication;
	obCommunication->inCheck        = inCheckTcpIpCommunication;
	obCommunication->inSend         = NULL; //inSendTcpIpCommunication;
	obCommunication->inReceive      = NULL; //inReceiveTcpIpCommunication;
	obCommunication->inEnd          = inEndTcpIpCommunication;
	obCommunication->inFlush        = inFlushTcpIpCommunication;
	obCommunication->inDeinitialize = inDeInitTcpIpCommunication;
	obCommunication->inSendReceive  = Send_Rcv_Pack;
}

void vdInitializeUclComm (COMM_OBJECT *obCommunication)
{
    obCommunication->inLinkType	= NO_COMM;  // see inside inInitUclComm...
    obCommunication->fLinkUp = VS_FALSE;
	obCommunication->inInitialize   = inInitUclCommunication;
	obCommunication->inBegin        = inBeginUclCommunication;
	obCommunication->inCheck        = inCheckUclCommunication;
	obCommunication->inSend         = inSendUclCommunication;
	obCommunication->inReceive      = inReceiveUclCommunication;
	obCommunication->inEnd          = inEndUclCommunication;
	obCommunication->inFlush        = inFlushUclCommunication;
	obCommunication->inDeinitialize = inDeInitUclCommunication;
	obCommunication->inSendReceive  = NULL;
}

int inInitModemCommunication (COMM_OBJECT *obCommunication)
{
   	MODEM_OBJECT *obModem;   	

	LOG_PRINTFF((0x08L, "inInitModeCommunication"));
   	obModem = (MODEM_OBJECT *)obCommunication->Data;
	
	if ((obCommunication->Data = (void *)pchAlloc(sizeof(MODEM_OBJECT))) == NULL_CH)
		return(VS_ERROR);

	obModem = (MODEM_OBJECT *)obCommunication->Data;
	LOG_PRINTFF((0x08L, "inInitModeCommunication....OK"));
	return(VS_SUCCESS);
}

int inInitTcpIpCommunication (COMM_OBJECT *obCommunication)
{
	InitTcpIp();
	return(VS_SUCCESS);
}

int inOpenModemCommunication (COMM_OBJECT *obCommunication, int inOpCode)
{
	MODEM_OBJECT *obModem;
	int inDataFormat;
	int inBautRate = Rt_2400;  //LIEM : consider as TDK_MODEM
	int inRetVal=VS_SUCCESS;

	obModem = (MODEM_OBJECT *)obCommunication->Data;
	
	if (inDefaultBaseInitModemEnvParam(obModem) == VS_ERR)
	{
        DisplayStatus("Open Modem Error!");
		return(VS_ERR);
	}
	
	inDataFormat = shGetDataFormat();
	return(VS_SUCCESS);
}

int inBeginModemCommunication (COMM_OBJECT *obCommunication, int inCPTIndex, VS_BOOL fUsePrimaryLine)
{
    int i;
    MODEM_OBJECT *obModem;
    
    obModem = (MODEM_OBJECT *)obCommunication->Data;
    
    for ( i=0; i < MAX_DIAL_LINES; ++i ) {
		obModem->inDialAttempts[i] = 0;
        obModem->fNoPhoneNumber[i] = VS_FALSE;
    }
    obModem->shSyncResultCode = -1;
    
    return ( inModemDial (obCommunication, fUsePrimaryLine) );
}

int inBeginTcpIpCommunication (COMM_OBJECT *obCommunication, int inCPTIndex, VS_BOOL fUsePrimaryLine)
{
	return (VS_SUCCESS);
}

int inCheckModemCommunication (COMM_OBJECT *obCommunication)
{
	int inMdmDialStatus, 
        inNumberOfTries,
        inCarrierTO,
        connectTimer,
        MsgType;
	long lnMsgNo = 0, event;
	unsigned int uiRedialDelay;
	VS_BOOL fABORT=VS_FALSE, fNextLine;
	MODEM_OBJECT *obModem;

	if (obCommunication->fLinkUp ==  VS_TRUE) {
		DisplayStatus("Still Connected");
		return(VS_SUCCESS);
	}

   	obModem = (MODEM_OBJECT *)obCommunication->Data;  	
    inNumberOfTries = shGetNumOfTries(); 
    uiRedialDelay = (unsigned int)lnGetRedialDelay();
    inCarrierTO = shGetCarrierTimeOut();
   		
    while (!fABORT){
      	do {
			vdDispLineDialing( obModem->inCurrentLine ); 
			connectTimer = set_timer((long)(inCarrierTO*1000L), EVT_TIMER);
            event = EVT_PIPE + EVT_KBD;
			while (TRUE) {
            //while( VS_TRUE ) {               
                if ( (event & EVT_KBD) != 0 ) {
                    if ( act_kbd_pending_test(KEY_CANCEL) ){
                        inMdmDialStatus = VS_ESCAPE;
                        break;
                    }
                }
                if ( (event & EVT_TIMER) != 0 ) {
                    inMdmDialStatus = NO_CARRIER;
                    break;
                }
                if ( (event & EVT_PIPE) != 0 ) {
					int inRetVal;
               		int inLocalTaskId = get_task_id();
					DIALER_RESP srDialerResp;
                	MEMCLR((char *)&srDialerResp, sizeof (DIALER_RESP));
					LOG_PRINTFF((0x08L, "ABOUT TO READ MODEM PIPE..."));
                	inRetVal = read(spipe, (char *)&srDialerResp, sizeof (DIALER_RESP));
					LOG_PRINTFF((0x08L, "READ MODEM PIPE...RETVAL=%d", inRetVal));
                	if (inRetVal > 0) { 
						LOG_PRINTFF((0x08L, "srDialerResp.shMdmDialStatus = %d", srDialerResp.shMdmDialStatus));
                    	inMdmDialStatus = (int)srDialerResp.shMdmDialStatus;
						LOG_PRINTFF((0x08L, "MODEM_DIAL_STATUS [1] = %d", inMdmDialStatus));
                        if ( obModem->hModem > 0 ) {
                        	if ( inGetModem (obModem->hModem, inLocalTaskId) != VS_SUCCESS ) {
                    	        inMdmDialStatus = MDM_DEV_CONFLICT;
                        	}
                        }
						LOG_PRINTFF((0x08L, "MODEM_DIAL_STATUS [2] = %d", inMdmDialStatus));
                        break;
                    }
	            }        
	            
                do {
                    event = wait_event ();
                } while ((event & EVT_PIPE) == 0 && (event & EVT_KBD) == 0 && (event & EVT_TIMER) == 0 );
            };
            
   			clr_timer(connectTimer);
   			fNextLine = VS_TRUE;
      		
			switch(inMdmDialStatus) {
      	   		case CONNECT_300:
      	   		case CONNECT_1200:
      	   		case CONNECT_2400:
      	   		case CONNECT_4800:  /* Hayes "CONNECT 4800" Status */
      	   		case CONNECT_9600:  /* Hayes "CONNECT 9600" Status */
      	   		case CONNECT_7200:  /* Hayes "CONNECT 7200" Status */
      	   		case CONNECT_12000:  /* Hayes "CONNECT 12000" Status */
      	   		case CONNECT_14400:  /* Hayes "CONNECT 14400" Status */
      	   		case CONNECT_19200:  /* Hayes "CONNECT 19200" Status */
      	   		case CONNECT_38400:  /* Hayes "CONNECT 38400" Status */
      	   		case CONNECT_57600:  /* Hayes "CONNECT 57600" Status */
      	   		case CONNECT_115200:  /* Hayes "CONNECT 115200" Status */
					{
						int xmdm_value;
#ifdef __arm
						xmdm_value = 1;
#else
						xmdm_value = xmdm_check_status(obModem->hModem, MDM_DCD);
#endif
     	   				if ( (xmdm_value == 1) && ((int)obCommunication->fLinkUp > VS_ERR) ){
							obCommunication->fLinkUp =  VS_TRUE;
							if( inMdmDialStatus == CONNECT_1200 )
	          	   				DisplayStatus("Connect at 1200...");
    						else if( inMdmDialStatus == CONNECT_2400 )
	          	   				DisplayStatus("Connect at 2400...");
							else	          	   		
	          	   				DisplayStatus("Connect at (--)...");
							SVC_WAIT (SHORT_PROMPT_DELAY);
          	   				return(VS_SUCCESS);
          				}
          				obCommunication->fLinkUp = VS_FALSE;
					}
              		break;
      	   		case HAYES_OK:
      	   			continue;
      	   		case VFI_DIALDONE:
      	   			DisplayStatus("Waiting for Answer...");
      	   			continue;
      	   		case VS_ESCAPE:
					obCommunication->inEnd(obCommunication);
					fABORT = VS_TRUE;
      	   			continue;
      	   		case VFI_NO_LINE:
				case CONNEXT_NO_LINE:
	       			fNextLine = VS_FALSE;
					DisplayStatus("No Line!");
					break;
      	   		case NO_DIALTONE:
					if (!(fGetBlindDial()))
						DisplayStatus("No Dial Tone!");
					else
						lnMsgNo = 0;
					break;
				case E_X_HR_TIMEOUT:
					DisplayStatus("Modem Timeout!");
					break;
      	   		case NO_CARRIER:
					DisplayStatus("No Carrier!");
					break;
	           	case E_HR_TIMEOUT:
		  	   	case NO_ANSWER:
			        DisplayStatus("No Answer!");
				    break;
	      	   	case BUSY_DETECT:
		            DisplayStatus("Busy!");
	                break;
	            case MDM_DEV_CONFLICT:
				case E_X_BAD_RESP:
   	       		case HAYES_ERROR:
           		case E_READ_CMD:
           		case E_ONLY_CR:
           		default:
					DisplayStatus("Comm Failure!");
					fABORT = VS_TRUE;
					continue;
   			}
            obCommunication->inEnd(obCommunication);   	        
            if ( !fABORT ) {
	            if (uiRedialDelay < 4000)
	        	    uiRedialDelay = 4000;
	            if ( inSmartDelay ((unsigned)uiRedialDelay) == VS_ESCAPE ) {
					DisplayStatus("Trx Cancelled!");
                    fABORT = VS_TRUE;
    	        }
       	        if ( fNextLine ) {
    	    	    if ( obModem->fNoPhoneNumber[++obModem->inCurrentLine] || obModem->inCurrentLine >= MAX_DIAL_LINES) {
		                break;
    		        }
        		}
                if ( inModemDial(obCommunication, VS_FALSE) != VS_SUCCESS ) {
                    fABORT = VS_TRUE;
                }
            }
        } while(obModem->inCurrentLine < MAX_DIAL_LINES && !fABORT);

        if ( !fABORT ) {
            obModem->inCurrentLine = PRIMARY_LINE;
	        if (obModem->fNoPhoneNumber[obModem->inCurrentLine])
	            obModem->inCurrentLine++;
			LOG_PRINTFF((0x08L, "Dial Attempts of line %d: %d", obModem->inCurrentLine, obModem->inDialAttempts[obModem->inCurrentLine]));
            if ( obModem->inDialAttempts[obModem->inCurrentLine] >= inNumberOfTries ) {
				LOG_PRINTFF((0x08L, "Dial Attempts have already %d", inNumberOfTries));
                break;
            }
            if ( inModemDial(obCommunication, VS_FALSE) != VS_SUCCESS ) {
                fABORT = VS_TRUE;
            }
        }
    };
    return(VS_ERR);
} /* End of inCheckModemCommunication() */

int inCheckTcpIpCommunication (COMM_OBJECT *obCommunication)
{   	
    obCommunication->fLinkUp =  VS_TRUE;
	DisplayStatus("Always Connected...");
    //SVC_WAIT (SHORT_PROMPT_DELAY);//kv1 debug, 25-0ct-2001
    return(VS_SUCCESS);
} /* End of inCheckModemCommunication() */

int inSendModemCommunication (COMM_OBJECT *obCommunication, char *pchSendBuff, int inSendSize, 
								VS_BOOL fConfirmSend, int inTimeout, long lnMessage)
{
	int     inRetVal;
	unsigned long ulDC2Ticks;
	MODEM_OBJECT *obModem;
	
    obModem = (MODEM_OBJECT *)obCommunication->Data;
	if ( obCommunication->fLinkUp != VS_TRUE ) {
	    return VS_CARRIER;
    }
	
	if (lnMessage != 0L) {
		DisplayStatus("Sending Data...");
	} else {
		DisplayStatus("Sending Reversal...");
	}
		
	inFlushModemCommunication();
	if ( (inRetVal = xmdm_send_data(obModem->hModem, pchSendBuff, inSendSize, inTimeout)) > 0 )	{
	    if ( fConfirmSend == VS_TRUE ) {
            ulDC2Ticks = set_itimeout(NO_CLOCK_HANDLE 30, TM_SECONDS );
	        while ( MDM_STATUS (obModem->hModem)&& CHECK_TIMEOUT(ulDC2Ticks )&& obCommunication->fLinkUp == VS_TRUE ) ;
            if ( obCommunication->fLinkUp != VS_TRUE ) {
				inRetVal = VS_CARRIER;
            } else  if ( !CHECK_TIMEOUT(ulDC2Ticks ) ) {   
	            inRetVal = VS_TIMEOUT;
	        }
	    }
	    inFlushModemCommunication ();
	} else {
	    inRetVal = inProcessPortError (obCommunication, inRetVal);
	}
	return(inRetVal);	
} /* End of inSendModemCommunication () */

int inSendTcpIpCommunication (COMM_OBJECT *obCommunication, char *pchSendBuff, int inSendSize, 
								VS_BOOL fConfirmSend, int inTimeout, long lnMessage)
{
	if ( obCommunication->fLinkUp != VS_TRUE ) {
	    return VS_CARRIER;
    }
	if (lnMessage != 0L) {
		DisplayStatus("Sending Data...");
	} else {
		DisplayStatus("Sending Reversal...");
	}
	return(inSendSize);
} /* End of inSendModemCommunication () */


int inReceiveModemCommunication (COMM_OBJECT *obCommunication, char *pchReceiveBuff, int inReceiveSize, 
								 int inReceiveTimeout, long lnMessage)
{
	int     inRetVal = VS_SUCCESS;
	MODEM_OBJECT *obModem;
   	obModem = (MODEM_OBJECT *)obCommunication->Data;

	if ( obCommunication->fLinkUp != VS_TRUE ) {
	    return VS_CARRIER;
    }
    if ( inRetVal == VS_SUCCESS ) {
		if (lnMessage != 0L) {
			DisplayStatus("Processing...");		
		} else {
			DisplayStatus("Processing Reversal..");
		}
		LOG_PRINTFF((0x08L, "Calling xmdm_receive_data()"));
        while ( (inRetVal = xmdm_receive_data(obModem->hModem, pchReceiveBuff, 0, inReceiveSize , inReceiveTimeout)) == 0 
                && obCommunication->fLinkUp == VS_TRUE );

        LOG_PRINTFF((0x08L, "xmdm_receive_data() inRetVal = %d", inRetVal));
		//LOG_PRINT_HEX((unsigned char *)pchReceiveBuff, 100);
        if ( obCommunication->fLinkUp != VS_TRUE ) {
            inRetVal = VS_CARRIER; 
        }
    	if ( inRetVal <= 0 ) {
    	    inRetVal = inProcessPortError (obCommunication, inRetVal);
    	}
	}
	return(inRetVal);
} /* End of inReceiveModemCommunication () */

int inReceiveTcpIpCommunication (COMM_OBJECT *obCommunication, char *pchReceiveBuff, int inReceiveSize, 
								 int inReceiveTimeout, long lnMessage)
{
	if ( obCommunication->fLinkUp != VS_TRUE ) {
	    return VS_CARRIER;
    }
	if (lnMessage != 0L) {
		DisplayStatus("Processing...");		
	} else {
		DisplayStatus("Processing Reversal..");
	}
	return(1);
} /* End of inReceiveModemCommunication () */


int inFlushModemCommunication ()
{
	int  inRetVal;
	MODEM_OBJECT *obModem;

   	obModem = (MODEM_OBJECT *)obCommunication.Data;
    inRetVal = xmdm_flush (obModem->hModem);
    return(inRetVal);
}

int inFlushTcpIpCommunication ()
{
	return(VS_SUCCESS);
}


int inEndModemCommunication (COMM_OBJECT *obCommunication)
{
	int     inRetVal=VS_SUCCESS;
	MODEM_OBJECT *obModem;

	LOG_PRINTFF((0x08L, "inEndModemCommunication"));
	obModem = (MODEM_OBJECT *)obCommunication->Data;	
	obCommunication->fLinkUp =  VS_FALSE;
	
	if ( obModem->hModem <= 0 ) {
		return(inRetVal);
	}
    inRetVal = inSyncDialTask (obModem, 0, VS_TRUE);        
    obModem->hModem	= 0;
	LOG_PRINTFF((0x08L, "inEndModemCommunication....OK"));
    return (inRetVal);
}

int inEndTcpIpCommunication (COMM_OBJECT *obCommunication)
{
	int inRetVal=VS_SUCCESS;
	obCommunication->fLinkUp =  VS_FALSE;
    return (inRetVal);    
}


int inDeInitModemCommunication (COMM_OBJECT *obCommunication)
{
   	MODEM_OBJECT *obModem;   	
   	obModem = (MODEM_OBJECT *)obCommunication->Data;
	
	LOG_PRINTFF((0x08L, "inDeInitModeCommunication"));
    vdFree ((char *)(obCommunication->Data));  
    obCommunication->inLinkType	= NO_COMM;
	
	memset((void *)obCommunication, 0x00, sizeof(COMM_OBJECT));
	LOG_PRINTFF((0x08L, "inDeInitModeCommunication....OK"));
	return ( VS_SUCCESS );
}

int inDeInitTcpIpCommunication (COMM_OBJECT *obCommunication)
{
	close(hLAN);
    obCommunication->inLinkType	= NO_COMM;	
	memset((void *)obCommunication, 0x00, sizeof(COMM_OBJECT));
	return ( VS_SUCCESS );
}

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
int	send_rev_cmd(char* sCmd, int iLenCmd, char* sRcv) 
{
	int iRead;
	unsigned long timeout;
	int iLen;
	int rc;

	sCmd[iLenCmd] = '\r';		// add CR character
	write(hLAN,sCmd,iLenCmd+1);

	timeout = read_ticks() + 5000;	// wait maximum 3 seconds for iChip response

	while (read_ticks() < timeout) {
		iRead = read(hLAN, sRcv, 1);
		if ((iRead == 1) && (sRcv[0] == 'I')) {
			iRead = read(hLAN, sRcv, 1);
			if ((iRead == 1) && (sRcv[0] == '/')) { 
				// case when "I/" received.. i.e. valid response
				iRead = read(hLAN, sRcv, 1);
				if ((sRcv[0] >= '0') && (sRcv[0] <= '9')) {
					// case when numeric
					iLen = sRcv[0] - '0';
					while (sRcv[0] != ':') {
						iRead = read(hLAN, sRcv, 1);
						if ((sRcv[0] < '0') || (sRcv[0] > '9'))
							break;	// non numeric - error
						iLen = iLen*10;	// multiple by 10;
						iLen = iLen + (sRcv[0] - '0');	// Add the last digit
					}
					if (sRcv[0] != ':')
						break;		// incorrect format, break to the outmost loop
					
					iRead = 0;
					do {
						rc = read(hLAN,&sRcv[iRead],iLen-iRead);
						iRead += rc;
					} while (iRead < iLen);

					return(iRead);
				} 
				else {
					iRead = read(hLAN,&sRcv[1],20) + 1;
					return (iRead);
				}
			}
		}
	}

	return -1;
}

int InitTcpIp ()
{
	char sCmd[128], sRcv[128];
	char buff[128];
	struct Opn_Blk ob;

	DisplayStatus("Init Tcp Ip...");

	memset(sCmd, '\0', sizeof(sCmd));
	memset(sRcv, '\0', sizeof(sRcv));
	memset(buff, '\0', sizeof(buff));
		
	if ( (hLAN = open("/dev/com3",0)) <= 0) 
	{
		DisplayStatus("Open IChipCOM error!");
		get_char();
		return VS_ERR;
	}

	ob.rate = Rt_9600;
	ob.format = Fmt_A8N1;
	ob.protocol = P_char_mode;

	if ( set_opn_blk(hLAN,&ob) < 0 ) 
	{
		DisplayStatus("Set open block error!");
		get_char();
		return VS_ERR;
	}

	DisplayStatus("Set Def IP...");
	strcpy(sCmd,"AT+iDIP:");
	strcat(sCmd,"0.0.0.0");
	memset(sRcv, '\0', sizeof(sRcv));
	send_rev_cmd(sCmd, strlen(sCmd), sRcv);

	DisplayStatus("Change Act IP...");
	strcpy(sCmd,"AT+iIPA:");
	get_env_x(UCL_LOCALIP, 0, buff);
	strcat(sCmd, buff);
	memset(sRcv, '\0', sizeof(sRcv));
	send_rev_cmd(sCmd, strlen(sCmd), sRcv);

	return VS_SUCCESS;
}

int Send_Rcv_Pack(char *SentBuff, int SentSz, char *RcvBuff, int *RcvSz)
{
	int iLenHeader;
	char sCmd[MSG_BUFF_SIZE+1], sRcv[MSG_BUFF_SIZE+1];
	char buff[MSG_BUFF_SIZE+1];
	unsigned long timeout;
	int RetVal=0;

	DisplayStatus("Get Host IP & Port...");
	
	memset(buff, 0x00, sizeof(buff));
	memset(sCmd, '\0', sizeof(sCmd));
	strcpy(sCmd,"AT+iSTCP:");
	get_env_x(UCL_HOSTIP, 0, buff);
	strcat(sCmd,buff);  //Host IP
	strcat(sCmd,",");
	memset(buff, 0x00, sizeof(buff));
	get_env_x(UCL_HOSTIP, 1, buff);
	strcat(sCmd,buff);
	memset(sRcv, '\0', sizeof(sRcv));
	send_rev_cmd(sCmd, strlen(sCmd), sRcv);

	memset(sCmd, '\0', sizeof(sCmd));
	strcpy(sCmd, "AT+iSDMP:0");
	memset(sRcv, '\0', sizeof(sRcv));
	send_rev_cmd(sCmd, strlen(sCmd), sRcv);

	DisplayStatus("Sending Data...");
	memset(sCmd, '\0', sizeof(sCmd));
	strcpy(sCmd,"AT+iSSND%:0,");
	sprintf(sRcv,"%d:",SentSz);
	strcat(sCmd,sRcv);
	iLenHeader = strlen(sCmd);
	memcpy(&sCmd[iLenHeader], SentBuff,SentSz);
	memset(sRcv, '\0', sizeof(sRcv));
	RetVal = send_rev_cmd(sCmd, SentSz + iLenHeader, sRcv);
	LOG_PRINTFF((0x08L, "Sending RetVal = %d", RetVal));

	//DisplayStatus("Receiving Data...");
	DisplayStatus("Processing...");
	timeout = read_ticks() + 30000;	
	LOG_PRINTFF((0x08L, "Receiving Begin At %ld", read_ticks()));
	while (read_ticks() < timeout && *RcvSz<1) {
		strcpy(sCmd,"AT+iSRCV:0,1028");
		*RcvSz = send_rev_cmd(sCmd, strlen(sCmd), RcvBuff);
	}
	LOG_PRINTFF((0x08L, "Receiving End At %ld", read_ticks()));
	strcpy(sCmd, "AT+iSCLS:0");
	memset(sRcv, '\0', sizeof(sRcv));
	send_rev_cmd(sCmd, strlen(sCmd), sRcv);

	return *RcvSz;
}


int inDefaultBaseInitModemEnvParam (MODEM_OBJECT *obModem)
{	
	int inModemType;
	int inResult;

	inResult = VS_SUCCESS;
	inModemType = SVC_INFO_MOD_ID();
		
	switch(inModemType) {
		case TDK_MODEM_ID:			
			inResult = inDefaultTDKInitModemEnvParam(obModem);
			LOG_PRINTFF((0x8L, "TDK Modem [%d] = %d", inModemType, inResult));
			break;
		case ASPEN_MODEM_ID:		
			inResult = inDefaultASPENInitModemEnvParam (obModem);
			LOG_PRINTFF((0x8L, "ASPEN Modem [%d] = %d", inModemType, inResult));
			break;
		case CONNEXANT_MODEM_ID:	
			inResult = inDefaultPREDATORInitModemEnvParam(obModem);
			LOG_PRINTFF((0x8L, "CONNEXANT Modem [%d] = %d", inModemType, inResult));
			break;
		default:					
			inResult = VS_ERROR;
			LOG_PRINTFF((0x8L, "UNKNOWN Modem [%d] = %d", inModemType, inResult));
			break;
	}	
	return inResult;
}

int inDefaultTDKInitModemEnvParam (MODEM_OBJECT *obModem)
{
	int result, str_len;
	char szModemInitString[MODEM_PARAM_SIZE+1];
	VS_BOOL fBell= fGetCommType();
	VS_BOOL fSync=(shGetDataFormat() == SDLC);

	put_env (SOFTPAY_BASE_MDM_ENV_VAR1, "", 0);
	put_env (SOFTPAY_BASE_MDM_ENV_VAR2, "", 0);
	put_env (MODEM_ENVIRONMENT_VARIABLE, "", 0);
	put_env (SYNC_MODEM_ENVIRONMENT_VARIABLE, "", 0);

	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
    strcat (szModemInitString, "E0V0");
	if (fGetBlindDial() == 0)
		strcat(szModemInitString, "X4");
	else
		strcat(szModemInitString, "X0");

	switch(shGetBaudRate()) {
		case BAUD_300:
		    if ( fBell )
    			strcat(szModemInitString, "S30=32");
    		else
    			strcat(szModemInitString, "S30=64");
			break;
        case AUTO_BAUD:
		case BAUD_2400:	
		    if ( fBell ) {
    			strcat(szModemInitString, "S30=52");
			} else
    			strcat(szModemInitString, "S30=204");
			break;
		default:
		case BAUD_1200:	
		    if ( fBell )
    			strcat(szModemInitString, "S30=16");
    		else
    			strcat(szModemInitString, "S30=8");
			break;
	}
	if (fGetModemFastConnect())
		strcat(szModemInitString, "S89+64");
	else			
		strcat(szModemInitString, "S89-64");

	str_len = strlen(szModemInitString);
	if ((result = put_env(SOFTPAY_BASE_MDM_ENV_VAR1, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }
	
	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
	vdGetModemParam(szModemInitString);
	str_len = strlen(szModemInitString);
	if ((result = put_env(MODEM_ENVIRONMENT_VARIABLE, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }
    
	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
    strcpy (szModemInitString, "K0Y1");
    str_len = strlen(szModemInitString);
    if ((result = put_env(SYNC_MODEM_ENVIRONMENT_VARIABLE, szModemInitString, str_len)) != str_len) {
    	return(result);
    }

	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
    // (W2) - Set modem to report DCE speed for CONNECT
	strcat(szModemInitString, "W2");
	// (K3) - Enable h/w flow control
	strcat(szModemInitString, "K3");  
	// (V0) - Numeric result codes
    strcat (szModemInitString, "V0"); 
    // (Y6) - applications do not need to change the comm rate based on connection results
    strcat (szModemInitString, "Y6");	//- factory default
	// (Q0) - Enable result codes
	strcat(szModemInitString, "Q0");    //- factory default

	str_len = strlen(szModemInitString);
	if ((result = put_env(ASYNC_MODEM_ENVIRONMENT_VARIABLE, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }
	return(VS_SUCCESS);
}

int inDefaultASPENInitModemEnvParam (MODEM_OBJECT *obModem)
{
	int result, str_len;
	char szModemInitString[MODEM_PARAM_SIZE+1];
	VS_BOOL fSync = (shGetDataFormat() == SDLC);

	put_env (SOFTPAY_BASE_MDM_ENV_VAR1, "", 0);
	put_env (SOFTPAY_BASE_MDM_ENV_VAR2, "", 0);
	put_env (MODEM_ENVIRONMENT_VARIABLE, "", 0);
	put_env (SYNC_MODEM_ENVIRONMENT_VARIABLE, "", 0);

	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
    strcat (szModemInitString, "&F");
    strcat (szModemInitString, "E0V0");
    strcat (szModemInitString, "&D2");
	strcat(szModemInitString, "%E0"); 
	strcat(szModemInitString, "\\N0");  
	str_len = strlen(szModemInitString);
	if ((result = put_env(SOFTPAY_BASE_MDM_ENV_VAR1, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }
    
	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
    if (fGetCommType())
        strcat(szModemInitString, "B1");   /* Set up for Bell */
    else
        strcat(szModemInitString, "B0");   /* Set up for CCITT */
	if (fGetBlindDial() == 0)
		strcat(szModemInitString, "X4S205=19");
	else
		strcat(szModemInitString, "X0S205=0");

	switch(shGetBaudRate())
	{
		case BAUD_300:
			strcat(szModemInitString, "F1");
			break;
		case BAUD_2400:	
			strcat(szModemInitString, "F5");
			break;
		case BAUD_4800:	
			strcat(szModemInitString, "F6");
			break;
		case BAUD_9600:	
			strcat(szModemInitString, "F8");
			break;
        case AUTO_BAUD:
            strcat(szModemInitString, "F0");
		case BAUD_1200:	
		default:
			strcat(szModemInitString, "F4");
			break;
	}

	if (fGetModemFastConnect())
		strcat(szModemInitString, "&H2");
	str_len = strlen(szModemInitString);
	if ((result = put_env(SOFTPAY_BASE_MDM_ENV_VAR2, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }

	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
	vdGetModemParam(szModemInitString);
	str_len = strlen(szModemInitString);
	if ((result = put_env(MODEM_ENVIRONMENT_VARIABLE, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }

	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
	strcat(szModemInitString, "W2");
    strcat (szModemInitString, "&M0");		
	str_len = strlen(szModemInitString);
	if ((result = put_env(ASYNC_MODEM_ENVIRONMENT_VARIABLE, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {				
		return(result);
    }
    
	MEMCLR(szModemInitString, MODEM_PARAM_SIZE);
//#ifdef DTR_DIAL		
//	strcat (szModemInitString, "&M2");
//#else
	strcat (szModemInitString, "&M1");
//#endif	
    strcat (szModemInitString, "S25=1");
    str_len = strlen(szModemInitString);
    if ((result = put_env(SYNC_MODEM_ENVIRONMENT_VARIABLE, szModemInitString, str_len)) != str_len) {
    	return(result);
    }
	return(VS_SUCCESS);
}

int inDefaultPREDATORInitModemEnvParam(MODEM_OBJECT* obModem) 
{
    int result, str_len;
    char szModemInitString[CONNEXANT_MODEM_PARAM_SIZE];
    VS_BOOL fSync = (shGetDataFormat() == SDLC);

    put_env(SOFTPAY_BASE_MDM_ENV_VAR1, "", 0);
    put_env(SOFTPAY_BASE_MDM_ENV_VAR2, "", 0);
    put_env(MODEM_ENVIRONMENT_VARIABLE, "", 0);
    put_env(SYNC_MODEM_ENVIRONMENT_VARIABLE, "", 0);
    put_env(ASYNC_MODEM_ENVIRONMENT_VARIABLE, "", 0);

    MEMCLR(szModemInitString, CONNEXANT_MODEM_PARAM_SIZE);
    strcat(szModemInitString, "&F");
    strcat(szModemInitString, "E0V0");
    strcat(szModemInitString, "&D2");
    if (fGetBlindDial() == 0) {    
		strcat (szModemInitString, "-STE=1");
    }
    str_len = strlen(szModemInitString);
    if ((result = put_env(SOFTPAY_BASE_MDM_ENV_VAR1, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {
        return(result);
    }

    MEMCLR(szModemInitString, CONNEXANT_MODEM_PARAM_SIZE);
    strcat(szModemInitString, "W2X4%C0\\N0+A8E=,,,0;");	
	if (fGetCommType())
		strcat(szModemInitString, "B1");   /* Set up for Bell */
	else
		strcat(szModemInitString, "B0");   /* Set up for CCITT */
    if (fGetBlindDial() == 1) {
        strcat(szModemInitString, "X0S6=0");
    }
    switch (shGetBaudRate()) {
        case Rt_300:
            strcat(szModemInitString, "+MS=V21,0,300,300,300,300;");
            break;
        case Rt_2400:	
            strcat(szModemInitString, "+MS=V22B,0,2400,2400,2400,2400;");
            break;
        case Rt_4800:	
            strcat(szModemInitString, "+MS=V32,0,4800,4800,4800,4800;");
            break;
        case Rt_9600:	
            strcat(szModemInitString, "+MS=V32,0,9600,9600,9600,9600;");
            break;
        case Rt_14400:
            strcat(szModemInitString, "+MS=V32B,0,14400,14400,14400,14400;");
            break;
        case Rt_1200:	
        default:
            strcat(szModemInitString, "+MS=V22,0,1200,1200,1200,1200;");
            break;
    }
    str_len = strlen(szModemInitString);
    if ((result = put_env(SOFTPAY_BASE_MDM_ENV_VAR2, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {
        return(result);
    }

    MEMCLR(szModemInitString, CONNEXANT_MODEM_PARAM_SIZE);
    vdGetModemParam(szModemInitString);
    str_len = strlen(szModemInitString);
    if ((result = put_env(MODEM_ENVIRONMENT_VARIABLE, szModemInitString, strlen(szModemInitString))) != strlen(szModemInitString)) {
        return(result);
    }

    MEMCLR(szModemInitString, CONNEXANT_MODEM_PARAM_SIZE); 
    if (fGetModemFastConnect()) {
		strcat(szModemInitString, "$F2");
    }
    strcat ( szModemInitString, "S38=2");
    strcat (szModemInitString, "S25=1");
    strcat (szModemInitString, "S17=15");
    strcat (szModemInitString, "+ES=6,,8;");
    strcat (szModemInitString, "+ESA=,,,,1");
    str_len = strlen(szModemInitString);
    if ((result = put_env(SYNC_MODEM_ENVIRONMENT_VARIABLE, szModemInitString, str_len)) != str_len) {
		return(result);
    }
    return(VS_SUCCESS);
}

int inProcessPortError (COMM_OBJECT *obCommunication, int inErr)
{
	int     inRetVal = VS_ERR;
	MODEM_OBJECT *obModem;
   	obModem = (MODEM_OBJECT *)obCommunication->Data;
   	
    switch ( inErr ) {
        case E_NOCARRIER:
        case E_X_NOCARRIER:
            obCommunication->fLinkUp =  VS_FALSE;
            inRetVal = VS_CARRIER;
            break;
        case 0:
        case E_HR_TIMEOUT:
        case E_X_HR_TIMEOUT:
        case VS_TIMEOUT:
            inRetVal = VS_TIMEOUT;
            break;
        case E_LATCH:
        case E_X_LATCH:
            MDM_RESET_ERROR (obModem->hModem);
            inFlushModemCommunication ();
            break;
        default:
            break;
    }
    return ( inRetVal );
} /* End of inProcessPortError () */

void vdDispLineDialing( int inCurrLine )
{
 	switch( inCurrLine ) {
 	case PRIMARY_LINE:
 		DisplayStatus("Dialing Primary...");
 		break;
 	case SECONDARY_LINE:
 		DisplayStatus("Dialing Secondary...");
 		break;
 	}
}

static int inGetPhoneNumber(MODEM_OBJECT *obModem, char *szPhoneNumber, int inCode)
{
	while(1) {
        if(obModem->inCurrentLine == PRIMARY_LINE) {
			vdGetPriAuthPhoneNumber(szPhoneNumber);
			if (!strlen(szPhoneNumber)) {
				obModem->fNoPhoneNumber[obModem->inCurrentLine] = VS_TRUE;
				obModem->inCurrentLine++;
			} else {
				break;
			}
		} else  {
			vdGetSecAuthPhoneNumber(szPhoneNumber);
			if (!strlen(szPhoneNumber)) {
				obModem->fNoPhoneNumber[obModem->inCurrentLine] = VS_TRUE;
				if (obModem->fNoPhoneNumber[obModem->inCurrentLine - 1] == VS_TRUE) {
					return(VS_ERR);
				} else {
					obModem->inCurrentLine--;
				}
			} else {
	    		break;
	    	}
	    }
	}//end while
	++obModem->inDialAttempts[obModem->inCurrentLine];
	LOG_PRINTFF((0x08L, "About to dial line [%d]: for [%d] time", obModem->inCurrentLine, obModem->inDialAttempts[obModem->inCurrentLine]));
	return (VS_SUCCESS);
}

static int inModemDial (COMM_OBJECT *obCommunication, VS_BOOL fUsePrimaryLine)
{
	MODEM_OBJECT *obModem;
	int inOpCode=0;
	int inDataFormat;
	char szPABXCode[PABX_CODE_SIZE +1];

	obModem = (MODEM_OBJECT *)obCommunication->Data;

	if ( obCommunication->fLinkUp == VS_TRUE ) {
		return(VS_SUCCESS);
    }
	if ( fUsePrimaryLine ) {
        obModem->inCurrentLine = PRIMARY_LINE;
	}
    if ( inOpenModemCommunication (obCommunication, inOpCode) == VS_ERR ) {
        return( VS_ERR );
    }    
	inDataFormat = (int)shGetDataFormat();
    strcpy( szPABXCode, szGetPABXCode() );
    if(obModem->inCurrentLine >= MAX_DIAL_LINES)
        obModem->inCurrentLine=PRIMARY_LINE;
	if ( inSyncDialTask(obModem, inOpCode, VS_FALSE) != VS_SUCCESS ) {
		return( VS_ERR );
   	}
	return(VS_SUCCESS);
} /* End of inModemDial () */

int inSyncDialTask (MODEM_OBJECT *obModem, int inOpCode, VS_BOOL fAbort)
{
	char pipe_data[10];
	SYNC_DIAL_OBJECT obSyncTask;

    obSyncTask.fAbort = fAbort;
	obSyncTask.shBaudRate = shGetBaudRate();
	obSyncTask.shDataFormat = shGetDataFormat(); 
    if ( !fAbort ) {
		strcpy( obSyncTask.szPABXCode, szGetPABXCode() );	
		if ( inGetPhoneNumber(obModem, obSyncTask.szPhoneNumber, inOpCode) == VS_ERR ) {
			return(VS_ERR);
		}
		obSyncTask.shModemMode = shGetModemMode(); 
		obSyncTask.shSyncSwitchDelay = shGetSyncSwitchDelay();
	    obSyncTask.fDialType = fGetDialType();
	    obSyncTask.fCommType = fGetCommType();
		if (SVC_INFO_MOD_ID() == TDK_MODEM_ID) {
			obSyncTask.fTDK_MODEM = VS_TRUE;
		} else {
			obSyncTask.fTDK_MODEM = VS_FALSE;
		}
	    obSyncTask.fAsync = (shGetDataFormat() != SDLC);
	}
		
	set_owner (obModem->hModem, remoteTaskID);
	SVC_WAIT(10);

    while ( read (spipe, pipe_data, sizeof (pipe_data)) > 0 ) ;

    if (write(spipe, (char *)&obSyncTask, sizeof(SYNC_DIAL_OBJECT)) < 0){
		return(VS_ERR);
    }				
	obModem->hModem = MDM_DEV_HANDLE;
	return(VS_SUCCESS);
}

int inSmartDelay ( unsigned int Delay )
{
	int     retVal = VS_SUCCESS;
    SVC_WAIT (Delay);
    return retVal;
}

int inGetModem (int h_modem, int inLocalTaskId)
{
	int     inRetVal=VS_ERR;
	int     inLoop=0;
	int     inOwnerTaskId;
	char    szDevName[DEVICE_NAME_SIZE];

	MEMCLR (szDevName, DEVICE_NAME_SIZE);
	strcpy (szDevName, MDM_PORT);
	do {
		h_modem = get_owner (szDevName, &inOwnerTaskId);
		if ( inLocalTaskId == inOwnerTaskId ) {
		    inRetVal = VS_SUCCESS;
		    break;
		}
		SVC_WAIT(20);
		++inLoop;
	} while ( inLoop <= 75 );  //kv1 debug - arbitrary loop setting
	return (inRetVal);
}

/*
MODEM=
szPriAuthPhoneNumber, 
szSecAuthPhoneNumber, 
shModemMode, 
szModemParam, 
shBaudRate, 
shDataFormat, 
shNumOfTries, 
lnRedialDelay,
lnMultiTransDelay, 
shCarrierTimeOut, 
shRespTimeOut, 
shInterCharTO, 
shSyncSwitchDelay
fReconnect, fModemFastConnect, fCommType, fBlindDial, fDialType, f3101CommType;
MODEM=5728788, 5728788, 13, S11=50S7=30, 2, 7, 3, 1000, 40, 90, 90, 0, 0, 011011
*/

short shGetDataFormat(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 5-2, buff);
	LOG_PRINTFF((0x08L, "shGetDataFormat (%s)", buff));
	return atoi(buff);
}

short shGetNumOfTries(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 6-2, buff);
	LOG_PRINTFF((0x08L, "shGetNumOfTries (%s)", buff));
	return atoi(buff);
}

long lnGetRedialDelay(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 7-2, buff);
	LOG_PRINTFF((0x08L, "shGetRedialDelay (%s)", buff));
	return atoi(buff);
}

short shGetCarrierTimeOut(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 9-2, buff);
	LOG_PRINTFF((0x08L, "shGetCarrierTimeOut (%s)", buff));
	return atoi(buff);
}

VS_BOOL fGetBlindDial(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 16-3, buff);
	LOG_PRINTFF((0x08L, "fGetBlindDial (%s)", buff));
	return atoi(buff);
}

VS_BOOL fGetCommType(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 15-3, buff);
	LOG_PRINTFF((0x08L, "fGetCommType (%s)", buff));
	return atoi(buff);
}

short shGetBaudRate(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 4-2, buff);
	LOG_PRINTFF((0x08L, "shGetBautRate (%s)", buff));
	return atoi(buff);
}

VS_BOOL fGetModemFastConnect(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 14-3, buff);
	LOG_PRINTFF((0x08L, "fGetModemFastConnect (%s)", buff));
	return atoi(buff);
}

void vdGetModemParam(char *szModemInitString)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 3-2, buff);
	LOG_PRINTFF((0x08L, "shGetModemParam (%s)", buff));
	strcpy(szModemInitString, buff);
}

void vdGetPriAuthPhoneNumber(char *szPhoneNumber)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_COMM, 0, buff);
	if (buff[0] != 'T') {
		memmove(&buff[1], buff, strlen(buff));
		buff[0] = 'T';
	}
	LOG_PRINTFF((0x08L, "vdGetPriAuth (%s)", buff));
	strcpy(szPhoneNumber, buff);  
}

void vdSetPriAuthPhoneNumber(char *szPhoneNumber)
{
	put_env_x(HOST_COMM, 0, szPhoneNumber);
	LOG_PRINTFF((0x08L, "vdSetPriAuth (%s)", szPhoneNumber));
}

void vdGetSecAuthPhoneNumber(char *szPhoneNumber)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_COMM, 1, buff);
	if (buff[0] != 'T') {
		memmove(&buff[1], buff, strlen(buff));
		buff[0] = 'T';
	}
	LOG_PRINTFF((0x08L, "vdGetSecAuth (%s)", buff));
	strcpy(szPhoneNumber, buff);  
}

void vdSetSecAuthPhoneNumber(char *szPhoneNumber)
{
	put_env_x(HOST_COMM, 1, szPhoneNumber);
	LOG_PRINTFF((0x08L, "vdSetSecAuth (%s)", szPhoneNumber));
}

char* szGetPABXCode(void)
{
	//return srTCTRec.szPABXCode;
	return "";
}

short shGetModemMode(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 2-2, buff);
	LOG_PRINTFF((0x08L, "shGetModemMode (%s)", buff));
	return atoi(buff);
}

short shGetSyncSwitchDelay(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 12-3, buff);
	LOG_PRINTFF((0x08L, "shGetSyncSwitchDelay (%s)", buff));
	return atoi(buff);
}

VS_BOOL fGetDialType(void)
{
	char buff[128];
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_MODEM, 17-3, buff);
	LOG_PRINTFF((0x08L, "fGetDialType (%s)", buff));
	return atoi(buff);
}

int inGetSendTimeOut(void)
{
	char buff[128];
	int TimeOut = 0;
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_COMM, 2, buff);
	TimeOut = atoi(buff)*100;
	if (TimeOut<=0 || TimeOut>=15000) TimeOut = SEND_COMM_TIMEOUT;
	return TimeOut;	
}

int inGetRecvTimeOut(void)
{
	char buff[128];
	int TimeOut = 0;
	memset(buff, 0x00, sizeof(buff));
	get_env_x(HOST_COMM, 2, buff);
	TimeOut = atoi(buff)*100;
	if (TimeOut<=0 || TimeOut>=15000) TimeOut = RECV_COMM_TIMEOUT;
	return TimeOut;	
}


